<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <CompileDependsOn Condition="'$(DesignTimeBuild)' != 'true'">$(CompileDependsOn);_CreateILLinkRuntimeRootDescriptorFile</CompileDependsOn>
  </PropertyGroup>

  <PropertyGroup>
    <_ILLinkRuntimeRootDescriptorFilePath>$(ILLinkDescriptorsXml)</_ILLinkRuntimeRootDescriptorFilePath>
    <_NamespaceFilePath Condition=" '$(_NamespaceFilePath)' == '' ">$(MSBuildThisFileDirectory)..\vm\namespace.h</_NamespaceFilePath>
    <_MscorlibFilePath Condition=" '$(_MscorlibFilePath)' == '' ">$(MSBuildThisFileDirectory)..\vm\corelib.h</_MscorlibFilePath>
    <_CortypeFilePath Condition=" '$(_CortypeFilePath)' == '' ">$(MSBuildThisFileDirectory)..\inc\cortypeinfo.h</_CortypeFilePath>
    <_RexcepFilePath Condition=" '$(_RexcepFilePath)' == '' ">$(MSBuildThisFileDirectory)..\vm\rexcep.h</_RexcepFilePath>
    <_ILLinkDescriptorsIntermediatePath>$(IntermediateOutputPath)ILLink.Descriptors.Combined.xml</_ILLinkDescriptorsIntermediatePath>
  </PropertyGroup>

  <ItemGroup>
    <_ILLinkDescriptorsFilePaths Include="$(ILLinkDirectory)ILLink.Descriptors.Shared.xml" />
    <_ILLinkDescriptorsFilePaths Condition="'$(TargetsWindows)' == 'true'" Include="$(ILLinkDirectory)ILLink.Descriptors.Windows.xml" />
    <_ILLinkDescriptorsFilePaths Include="$(ILLinkDirectory)ILLink.Descriptors.Debug.xml"
                                 Condition="'$(Configuration)' == 'Debug' or '$(Configuration)' == 'Checked'" />
    <_ILLinkDescriptorsFilePaths Include="$(CoreLibSharedDir)ILLink\ILLink.Descriptors.Shared.xml" />
  </ItemGroup>

  <!--
        Generates the xml root descriptor file for ILLink. The file contains roots required by the runtime.
        This gets generated by the linker.
        If it fails, it'll spit out some cryptic error messages that's hard to debug.
        You can debug it by setting /maxcpucount:1 in the msbuild command to force it to not spawn up a bunch
        of child processes, and attach a debugger to msbuild. After that you can set a bp on the exception
        you are seeing.
  -->
  <UsingTask TaskName="CreateRuntimeRootILLinkDescriptorFile"
             AssemblyFile="$(ILLinkTasksAssembly)"
             TaskFactory="TaskHostFactory"
             Condition="'$(ILLinkTasksAssembly)' != ''" />
  <Target Name="_CreateILLinkRuntimeRootDescriptorFile"
          DependsOnTargets="_CombineILLinkTrimXmlFilePaths"
          Inputs="$(_NamespaceFilePath);$(_MscorlibFilePath);$(_CortypeFilePath);$(_RexcepFilePath);$(_ILLinkDescriptorsIntermediatePath)"
          Outputs="$(_ILLinkRuntimeRootDescriptorFilePath)">
    <Message Text="Generating ILLink roots file: $(_ILLinkRuntimeRootDescriptorFilePath)" />
    <CreateRuntimeRootILLinkDescriptorFile NamespaceFilePath="$(_NamespaceFilePath)"
                                           MscorlibFilePath="$(_MscorlibFilePath)"
                                           CortypeFilePath="$(_CortypeFilePath)"
                                           RexcepFilePath="$(_RexcepFilePath)"
                                           ILLinkTrimXmlFilePath="$(_ILLinkDescriptorsIntermediatePath)"
                                           DefineConstants="$(DefineConstants);FOR_ILLINK"
                                           RuntimeRootDescriptorFilePath="$(_ILLinkRuntimeRootDescriptorFilePath)" />
  </Target>

  <UsingTask TaskName="CombineLinkerXmlFiles"
             AssemblyFile="$(ILLinkTasksAssembly)"
             TaskFactory="TaskHostFactory"
             Condition="'$(ILLinkTasksAssembly)' != ''" />
  <Target Name="_CombineILLinkTrimXmlFilePaths"
          Inputs="@(_ILLinkDescriptorsFilePaths)"
          Outputs="$(_ILLinkDescriptorsIntermediatePath)">

    <CombineLinkerXmlFiles LinkerXmlFiles="@(_ILLinkDescriptorsFilePaths)"
                           CombinedLinkerXmlFile="$(_ILLinkDescriptorsIntermediatePath)" />

    <ItemGroup>
      <FileWrites Include="$(_ILLinkDescriptorsIntermediatePath)" />
    </ItemGroup>
  </Target>

</Project>
